package com.sy.util;

import com.aliyun.oss.model.OSSObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

public class LoadProties {

    public static Properties[] getProties() throws IOException {
        File[] files = new File(Const.MailPath.imapPath).listFiles();// 加载imap目录配置文件
        Properties[] props = new Properties[files.length]; // 初始化properties配置文件
        int i = 0;
        for (File file : files) {
            if (!file.isDirectory()) {
                // 准备连接服务器的会话信息
                props[i] = new Properties();
                FileInputStream input = new FileInputStream(file.getPath());
                props[i].load(input);
                props[i].setProperty("mail.store.protocol", "imap");
                props[i].setProperty("mail.imap.ssl.enable", "true");
                props[i].setProperty("mail.imap.timeout", "10000");
                input.close();
            }
            i++;
        }
        return props;
    }

    public static Properties[] getOSSProties() throws IOException {
        OSSUtil ossUtil = new OSSUtil();
        OSSObject[] ossObjects = ossUtil.readDir(Const.OSSPath.PROPERTIES);
        Properties[] props = new Properties[ossObjects.length]; // 初始化properties配置文件
        int i = 0;
        for (OSSObject properties : ossObjects) {
            if (properties != null) {
                // 准备连接服务器的会话信息
                props[i] = new Properties();
                InputStreamReader input = new InputStreamReader(properties.getObjectContent());
                props[i].load(input);
                props[i].setProperty("mail.store.protocol", "imap");
                props[i].setProperty("mail.imap.ssl.enable", "true");
                props[i].setProperty("mail.imap.timeout", "10000");
                input.close();
                properties.close();
                i++;
            }
        }
        ossUtil.shutdown();
        return props;
    }
}
